package startup;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

import javax.swing.JOptionPane;

import businesslogic.*;

public class Network {
	
	public static void init(String username, String password){
		try{
			BookBL bookBL = new BookBL(username, password);
			CustomerBL customerBL = new CustomerBL(username, password);
			OrderBL orderBL = new OrderBL(username, password);
			PromotionBL promotionBL = new PromotionBL(username, password);
			SalesBL salesBL = new SalesBL(username, password);
			UserBL userBL = new UserBL(username, password); 
			
			if(bookBL.getCategorys().size()==0)
				bookBL.initCategorys();
			if(promotionBL.getStrategy()==null)
				promotionBL.initStrategy();
			
			LocateRegistry.createRegistry(8888);
			
			Naming.bind("rmi://localhost:8888/bookBL",bookBL);
			Naming.bind("rmi://localhost:8888/customerBL",customerBL);
			Naming.bind("rmi://localhost:8888/orderBL",orderBL);
			Naming.bind("rmi://localhost:8888/promotionBL",promotionBL);
			Naming.bind("rmi://localhost:8888/salesBL",salesBL);
			Naming.bind("rmi://localhost:8888/userBL",userBL);

        } catch (RemoteException e) { 
        	JOptionPane.showMessageDialog(null, "RemoteException"); 
        } catch (AlreadyBoundException e) { 
        	JOptionPane.showMessageDialog(null, "AlreadyBoundException");
        } catch (MalformedURLException e) { 
        	JOptionPane.showMessageDialog(null, "MalformedURLException");
        } 
	}
}
